/**
 * @Title:  PersonalDataActivity.java 
 * @Copyright:  XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved 
 * @Description:  TODO<请描述此文件是做什么的> 
 * @author:  MeiYi 
 * @data:  2016-4-15 下午2:17:28 
 * @version:  V1.0 
 */

package artid.com.arttogo.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;

import java.util.ArrayList;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.StrericWheelAdapter;
import artid.com.arttogo.bean.AppItem;
import artid.com.arttogo.bean.PeopleList;
import artid.com.arttogo.bean.PersonalCenter;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.AlertUtils;
import artid.com.arttogo.utils.Config;
import artid.com.arttogo.utils.MySharedPreferences;
import artid.com.arttogo.view.WheelView;

/**
 * 个人资料
 * 
 * @author 熊彬
 * @data: 2016-4-15 下午2:17:28
 * @version: V1.0
 */
public class PersonalDataActivity extends BaseActivity implements
		OnClickListener {
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sigmatrix.artworkart.BaseActivity#onCreate(android.os.Bundle)
	 */

	LinearLayout mlineSf, mlineYh, mlineGh, mlineCq, mlineMzp, mlineYs;
	// 下方显示的喜爱偏好选项块
	TextView mtextSf, mtextYh, mtextGh, mtextCq, mtextMzp, mtextYs, mtextSex,
			mtextIncome, mtextIndus;
	EditText mtextName;
	Boolean isSfCheck = false, isYhCheck = false, isGhCheck = false,
			isCqCheck = false, isMzpCheck = false, isYsCheck = false;
	DatePicker datepicker;
	View date, dateView;
	TextView textdate;
	Button btnSetDate;
	View sex, income,indus;
	WheelView wheelView;
	MySharedPreferences share;
	ArrayList<PeopleList.item> listIncome;
	ArrayList<PeopleList.item> listLike;
	ArrayList<PeopleList.item> listIndustry;
	final int TAG_INCOME = 0, TAG_LIKE = 1, TAG_INDUS = 2;
	String strIdLike = "",strIdIndus = "",strIdIncome = "",strIdSex = "";
	TextView textSave;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_personal_data);
		mTittle = (TextView)findViewById(R.id.tittle_text);
		mTittle.setText("个人资料");
		mBack = (ImageView)findViewById(R.id.back_btn);
		mBack.setOnClickListener(this);
		mtextIncome = (TextView) findViewById(R.id.text_income);
		mtextIndus = (TextView) findViewById(R.id.text_indus);
		income = (View) findViewById(R.id.rela_income);
		income.setOnClickListener(this);
		indus = (View)findViewById(R.id.rela_indus);
		indus.setOnClickListener(this);
		datepicker = (DatePicker) findViewById(R.id.datePicker1);
		dateView = (View) findViewById(R.id.date_view);
		dateView.setOnClickListener(this);
		dateView.setVisibility(View.GONE);
		btnSetDate = (Button) findViewById(R.id.person_btn);
		btnSetDate.setOnClickListener(this);
		date = (View) findViewById(R.id.item_view_date);
		date.setOnClickListener(this);
		textdate = (TextView) findViewById(R.id.text_personcenter_date);
		sex = (View) findViewById(R.id.rela_sex);
		sex.setOnClickListener(this);
		mtextSex = (TextView) findViewById(R.id.text_sex);
		mtextName = (EditText) findViewById(R.id.text_name);
		textSave = (TextView)findViewById(R.id.perdata_save);
		textSave.setOnClickListener(this);
		mlineSf = (LinearLayout) findViewById(R.id.item_person_sf);
		mlineSf.setOnClickListener(this);
		mlineYh = (LinearLayout) findViewById(R.id.item_person_yx);
		mlineYh.setOnClickListener(this);
		mlineGh = (LinearLayout) findViewById(R.id.item_person_gh);
		mlineGh.setOnClickListener(this);
		mlineCq = (LinearLayout) findViewById(R.id.item_person_cq);
		mlineCq.setOnClickListener(this);
		mlineMzp = (LinearLayout) findViewById(R.id.item_person_mzp);
		mlineMzp.setOnClickListener(this);
		mlineYs = (LinearLayout) findViewById(R.id.item_person_ys);
		mlineYs.setOnClickListener(this);
		mtextSf = (TextView) findViewById(R.id.text_item_person_sf);
		mtextYh = (TextView) findViewById(R.id.text_item_person_yh);
		mtextGh = (TextView) findViewById(R.id.text_item_person_gh);
		mtextCq = (TextView) findViewById(R.id.text_item_person_cq);
		mtextMzp = (TextView) findViewById(R.id.text_item_person_mzp);
		mtextYs = (TextView) findViewById(R.id.text_item_person_ys);
		reqData();
		
		 
	}
	void reqData() {
		// System.out.println("token=" + AppUtil.getUser().getToken());
		VolleyBasic.getInstance(PersonalDataActivity.this).post(
				Cache.personalCenter,
				VolleyMap.getInstance().personalCenter(
						AppUtil.getUser().getToken()), PersonalCenter.class,

				new VolleyCallback() {

					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
						if (vd.getCode() == -99) {
							AppUtil.Logout(PersonalDataActivity.this);
							AppUtil.LoginVer(PersonalDataActivity.this);
							return;
						}
						PersonalCenter pc = (PersonalCenter) vd.getData();
						if (!TextUtils.isEmpty(pc.getRealname())) {
							mtextName.setText(pc.getRealname());
						}
						if (!TextUtils.isEmpty(pc.getBirthday())&&!pc.getBirthday().contains("0000")) {
							textdate.setText(pc.getBirthday());
						}
						if (!TextUtils.isEmpty(pc.getSex())) {
							if (pc.getSex().equals("0")) {
								strIdSex = "0";
								mtextSex.setText("女");
							} else {
								strIdSex ="1";
								mtextSex.setText("男");
							}
						}
						strIdIncome = pc.getIncomeid();
						strIdIndus = pc.getIndustryid();
						likeid = pc.getLikeid();
						String[] arrlike = likeid.split(",");
						for (int i = 0; i < arrlike.length; i++) {
							try {
								int id = Integer.valueOf(arrlike[i]);
								switch (id) {
								case 1:
									isSfCheck =true;
									setLike(true,mtextSf,mlineSf);
									break;
								case 2:
									isYhCheck =true;
									setLike(true,mtextYh,mlineYh);
									break;
								case 3:
									isGhCheck =true;
									setLike(true,mtextGh,mlineGh);
									break;
								case 4:
									isCqCheck =true;
									setLike(true,mtextCq,mlineCq);
									break;
								case 5:
									isMzpCheck =true;
									setLike(true,mtextMzp,mlineMzp);
									break;
								case 6:
									isYsCheck =true;
									setLike(true,mtextYs,mlineYs);
									break;
								default:
									break;
								}	
							} catch (Exception e) {
								// TODO: handle exception
							}
						}
						reqBasicListData();
					}

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub

					}
				});
	}
	
	void reqModify(){
		likeid = "";
		if (isSfCheck) {
			likeid+="1,";
		}
		if (isYhCheck) {
			likeid+="2,";
		}if (isGhCheck) {
			likeid+="3,";
		}if (isCqCheck) {
			likeid+="4,";
		}if (isMzpCheck) {
			likeid+="5,";
		}if (isYsCheck) {
			likeid+="6,";
		}
		if (!TextUtils.isEmpty(likeid)) {
			likeid = likeid.substring(0, likeid.length()-1);
		}
		VolleyBasic.getInstance(PersonalDataActivity.this).post(Cache.modifyInfo, 
				VolleyMap.getInstance().modifyInfo(mtextName.getText().toString(),
						textdate.getText().toString(),strIdSex,strIdIncome,strIdIndus,likeid), AppItem.class,
				new VolleyCallback() {
					
					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
						Config.showTip(PersonalDataActivity.this, vd.getMsg());
						if (vd.getCode()==VolleyData.Success) {
							finish();
						}else {
						}
							
					}
					
					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						
					}
				});
	}
	
	
	void reqBasicListData() {
		VolleyBasic.getInstance(PersonalDataActivity.this).post(
				Cache.peopleList, VolleyMap.getInstance().peopleList(),
				PeopleList.class, new VolleyCallback() {

					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
						PeopleList item = (PeopleList) vd.getData();
						listIncome = item.getListIncome();
						listIndustry = item.getListIndustry();
						listLike = item.getListLike();
						for (int i = 0; i < listIncome.size(); i++) {
							String incomId = listIncome.get(i).getId();
							if (incomId.equals(strIdIncome)) {
								mtextIncome.setText(listIncome.get(i).getName());
							}
						}
						for (int i = 0; i < listIndustry.size(); i++) {
							String indusId = listIndustry.get(i).getId();
							if (indusId.equals(strIdIndus)) {
								mtextIndus.setText(listIndustry.get(i).getName());
							}
						}
						
					}
					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub

					}
				});
	}

	String[] arrData(ArrayList<PeopleList.item> listin) {
		String all = "";
		if (listin.isEmpty()) {
			Toast.makeText(PersonalDataActivity.this, "检查网络设置", 300).show();
			return null;
		}
		for (int i = 0; i < listin.size(); i++) {
			PeopleList.item item = listin.get(i);
			all += item.getName() + ",";
		}
		return all.split(",");
	}
	String[] timeData;
	ArrayList<PeopleList.item> listin;
	@SuppressWarnings("deprecation")
	@SuppressLint("InflateParams")
	private void initAlertDialog(final int tag) {
		View view = LayoutInflater.from(PersonalDataActivity.this).inflate(
				R.layout.pop_menu_type, null);
		switch (tag) {
		case TAG_INCOME:
			listin = listIncome;

			break;
		case TAG_INDUS:
			listin = listIndustry;
			break;
		default:
			break;
		}
		timeData = arrData(listin);
		wheelView = (WheelView) view.findViewById(R.id.timeWheel);
		wheelView.setAdapter(new StrericWheelAdapter(timeData));
		wheelView.setCurrentItem(2);
		wheelView.setCyclic(true);
		wheelView.setInterpolator(new AnticipateOvershootInterpolator());

		view.findViewById(R.id.btnCancel).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (alertDialog != null && alertDialog.isShowing()) {
							alertDialog.dismiss();
						}
					}
				});
		view.findViewById(R.id.btnConfirm).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						final int select = Integer.valueOf(wheelView
								.getCurrentItem());
						if (alertDialog != null && alertDialog.isShowing()) {
							switch (tag) {
							case TAG_INCOME:
								mtextIncome.setText(timeData[select]);
								strIdIncome = listin.get(select).getId();
								break;
							case TAG_INDUS:
								mtextIndus.setText(timeData[select]);
								strIdIndus = listin.get(select).getId();
								break;
							default:
								break;
							}

							alertDialog.dismiss();
						}
					}
				});

		alertDialog = new AlertDialog.Builder(PersonalDataActivity.this)
				.create();
		alertDialog.show();

		WindowManager.LayoutParams params = alertDialog.getWindow()
				.getAttributes();
		params.width = PersonalDataActivity.this.getWindowManager()
				.getDefaultDisplay().getWidth();
		int height = PersonalDataActivity.this.getWindowManager()
				.getDefaultDisplay().getHeight();
		params.height = LayoutParams.WRAP_CONTENT;
		params.gravity = Gravity.BOTTOM;

		Window window = alertDialog.getWindow();
		window.setAttributes(params);
		window.setContentView(view);
		alertDialog.getWindow().setContentView(view);

	}

	AlertDialog alertDialog;
	AlertUtils alertUtils;
	Boolean isMan = true;

	@SuppressLint("NewApi")
	void setLike(Boolean isLike, TextView text, View view){
		if (isLike) {
			text.setTextColor(Color.WHITE);
			view.setBackground(getResources()
					.getDrawable(R.color.black));
		}else {
			text.setTextColor(getResources().getColor(R.color.oragne));
			view.setBackground(getResources().getDrawable(
					R.drawable.guanzhu_frame));
		}
	}
	
	String likeid = "";
	@SuppressLint("NewApi")
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

		switch (arg0.getId()) {
		case R.id.item_view_date:
			if (dateView.getVisibility() == View.VISIBLE) {
				dateView.setVisibility(View.GONE);
			} else {
				dateView.setVisibility(View.VISIBLE);
			}
			break;

		case R.id.item_person_sf:
			if (!isSfCheck) {
				setLike(true,mtextSf,mlineSf);
				isSfCheck = true;
			}else {
				setLike(false,mtextSf,mlineSf);
				isSfCheck = false;
			}
			break;
		case R.id.item_person_yx:
			if (!isYhCheck) {
				setLike(true,mtextYh,mlineYh);
				isYhCheck = true;
			}else {
				setLike(false,mtextYh,mlineYh);
				isYhCheck = false;
			}
			break;
		case R.id.item_person_gh:
			if (!isGhCheck) {
				setLike(true,mtextGh,mlineGh);
				isGhCheck = true;
			}else {
				setLike(false,mtextGh,mtextGh);
				isGhCheck = false;
			}
			break;
		case R.id.item_person_cq:
			if (!isCqCheck) {
				setLike(true,mtextCq,mlineCq);
				isCqCheck = true;
			}else {
				setLike(false,mtextCq,mlineCq);
				isCqCheck = false;
			}
			break;
		case R.id.item_person_mzp:
			if (!isMzpCheck) {
				setLike(true,mtextMzp,mlineMzp);
				isMzpCheck = true;
			}else {
				setLike(false,mtextMzp,mlineMzp);
				isMzpCheck = false;
			}
			break;
		case R.id.item_person_ys:
			if (!isYsCheck) {
				setLike(true,mtextYs,mlineYs);
				isYsCheck = true;
			}else {
				setLike(false,mtextYs,mlineYs);
				isYsCheck = false;
			}
			break;
		case R.id.person_btn:
			dateView.setVisibility(View.GONE);
			textdate.setText(datepicker.getYear() + "-"
					+ (datepicker.getMonth() + 1) + "-"
					+ datepicker.getDayOfMonth() );
			break;
		case R.id.rela_sex:
			alertUtils = new AlertUtils();
			alertDialog = alertUtils.initPhotoAlertDialog(
					PersonalDataActivity.this,
					new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub

							switch (arg0.getId()) {
							case R.id.text_pop_ok:
								isMan = alertUtils.isman;
								alertDialog.dismiss();
								if (isMan) {
									mtextSex.setText("男");
									strIdSex = "1";
								} else {
									mtextSex.setText("女");
									strIdSex = "0"; 
								}
								break;
							default:
								break;
							}
						}
					});
			alertDialog.show();
			break;
		case R.id.date_view:
			dateView.setVisibility(View.GONE);
			break;
		case R.id.rela_income:
			initAlertDialog(TAG_INCOME);
			break;
		case R.id.rela_indus:
			initAlertDialog(TAG_INDUS);
			break;
		case R.id.perdata_save:
			reqModify();
			break;
		case R.id.back_btn:
			finish();
			break;
		default:
			break;
		}
	}
}
